package com.stepshop.junit;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import com.stepshop.SysOutThread;
import com.stepshop.core.thread.jdk.JDKExecutor;
import com.stepshop.sys.bean.SysArea;
import com.stepshop.sys.bean.SysUser;
import com.stepshop.sys.service.SysAreaService;
import com.stepshop.sys.service.SysUserService;

public class JunitTestExecutor extends JunitBase{
	private Logger logger=Logger.getLogger(this.getClass());
	@Resource
	private ThreadPoolTaskExecutor springExecutor;
	@Resource
	private SysAreaService sysAreaService;
	@Resource
	private SysUserService sysUserService;
	@Test
	public void test1(){
//		ThreadPoolTaskExecutor springExecutor = (ThreadPoolTaskExecutor) ac.getBean("springExecutor");
		for(int i=0;i<1000;i++){
			SysOutThread run=new SysOutThread();
			logger.info("springExecutor.getCorePoolSize:"+springExecutor.getCorePoolSize());
			springExecutor.getCorePoolSize();
			springExecutor.execute(run);
		}
		System.out.println("main.JDKExecutor.count:"+JDKExecutor.count);
	}
	@Test
	public void test2(){
		List<SysArea> all = sysAreaService.queryAll();
		Map<String,Object> map=new HashMap<String,Object>();
		for(SysArea area:all){
			map.put(area.getCode()+"",area);
			logger.info(area.getName()+"");
		}
		logger.info(map);
	}
	@Test
	public void test3(){
		SysArea area = sysAreaService.queryById(1l);
		System.out.println(area.getName());
	}
	@Test
	public void test4(){
		List<SysArea> all = sysAreaService.queryAll();
		for(SysArea area:all){
			logger.info("----id:"+area.getId()+"||name:"+area.getName());
		}
	}
	@Test
	public void test5(){
		SysArea area = sysAreaService.queryAreaById(1l);
		logger.info("id:"+area.getId()+"||name:"+area.getName());
	}
	@Test 
	public void testUserInsert(){
		SysUser user=new SysUser();
		user.setName("小黑ieee");
		sysUserService.add(user);
	}
	@Test
	public void testUserSelect(){
		List<SysUser> all = sysUserService.queryAll();
		printList(all);
	}
	private void printList(List list){
		try {
			if (list != null && !list.isEmpty()) {
				Iterator it = list.iterator();
				while (it.hasNext()) {
					Object obj = it.next();
					Field[] fields = obj.getClass().getDeclaredFields();
					for (Field field : fields) {
						logger.info(field.getName() + "[" + BeanUtils.getProperty(obj, field.getName()) + "]");
					}
				}
			} 
		} catch (Exception e) {
		}
	}
}
